/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package no.ffi.tpm;

import java.util.logging.Level;
import java.util.logging.Logger;
import no.ffi.tpm.aik.AIKUtils;
import  no.ffi.gismoidm.idp.Config;
import no.ffi.tpm.aik.AIKRequest;
import no.ffi.tpm.crypto.EncryptedObject;

/**
 * Create the AIK for a new user and the corresponding property file
 * @author Federico Mancini <fma at ffi.no>
 */
public class CreateNewUser {
    public static void main(String[] args){
       create("Federico","AIKPass","TPMpassword","EdwardFongen");
       // resume("Federico","TPMpassword",AIKUtils.RESPONSE,"5be25d88-52ba-4688-886b-8a40e6d14db3","EdwardFongen","AIKPass");
    }

    private static void create(String userName, String aikPass, String TPMpass, String keyStore)  {
       
        System.setProperty("GISMOIDM.CONFIG","/home/tomcat7/config/gismoidm.properties");
        Config.loadPropertiesFrom(null);
       String trustAnchorPemFilename = Config.getProperty("CA-CERT");
       EncryptedObject req=AIKUtils.createAIKRequest(trustAnchorPemFilename, aikPass, TPMpass);
       
       EncryptedObject resp=null;
       Config.setProperty("AIK_UUID", AIKUtils.uuid);
       Config.setProperty("AIK_PASS",aikPass);
       try {
           // AIKUtils.writeToFile(req,AIKUtils.REQUEST);
            resp=AIKUtils.certifyAIK(req);
            //AIKUtils.writeToFile(resp,AIKUtils.RESPONSE);
        } catch (Exception ex) {
            try {
//                AIKUtils.writeToFile(req,AIKUtils.REQUEST);
                System.out.println("Something went wrong with AIK certification: "+ex.getMessage()+", writing AIKrequest to file");
            } catch (Exception ex1) {
                Logger.getLogger(CreateNewUser.class.getName()).log(Level.SEVERE, null, ex1);
                return;
            }
            
        } try {
            String secret=AIKUtils.activateAIK(resp, TPMpass,aikPass);
            System.out.println("AIK-PASS"+aikPass+"AIK-SNR"+secret);
            
            Config.setProperty("AIK_SNR",secret);
            //Config.setProperty("USER-KEYSTORE", keyStore+".p12");
            //Config.setProperty("USER-CERT", keyStore+".pem");
            //Config.setProperty("USER-KEYSTORE-PASS", "anders");
            System.out.println("AIK creation successful");
        } catch (Exception ex){
            try {
                AIKUtils.writeToFile(resp,AIKUtils.RESPONSE);
                System.out.println("Something went wrong with AIK certification, writing AIKrequest to file");
            } catch (Exception ex1) {
                Logger.getLogger(CreateNewUser.class.getName()).log(Level.SEVERE, null, ex1);
            }
                
    }
}
    
    private static void resume(String userName, String TPMpass,String type, String uuid, String keyStore, String AIKpass)  {
        try {
            //load config file, retrieve uuid, retrieve REQ or RESP and complete the AIK
            //certification
            EncryptedObject resp=null;
            EncryptedObject aikObject = (EncryptedObject) AIKUtils.getStoredAIKFiles(uuid, type);
            Config.loadPropertiesFrom(null);
            Config.setProperty("AIK-UUID", uuid);
            Config.setProperty("AIK-PASS",AIKpass);
            Config.setProperty("USER-KEYSTORE", keyStore+".p12");
            Config.setProperty("USER-CERT", keyStore+".pem");
            Config.setProperty("USER-KEYSTORE-PASS", "anders");
            if(type.equals(AIKUtils.REQUEST)){
                try {
                    resp=AIKUtils.certifyAIK(aikObject);
                } catch (Exception ex) {
                   System.out.println("Something went wrong with AIK certification: "+ex.getMessage()+", writing AIKrequest to file");
                   return;
                } 
            }
            if(type.equals(AIKUtils.RESPONSE)){ 
                    resp=aikObject;
            } 
            String secret=AIKUtils.activateAIK(resp, TPMpass,AIKpass);
            System.out.println("AIK-PASS"+AIKpass+"AIK-SNR"+secret);
            Config.setProperty("AIK-SNR",secret);
        } catch (Exception ex) {
            Logger.getLogger(CreateNewUser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
